package com.qf.flash.ui.business;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.qf.flash.R;
import com.qf.flash.http.response.GameDetailResponse;
import com.qf.flash.mvp.contract.GameMemberContract;
import com.qf.flash.mvp.presenter.GameMemberPresenter;
import com.qf.flash.ui.business.adapter.MoneyRecodeItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2020/3/15 on 15:18
 * 描述: 我的收款账单
 * 作者:戴旭东
 */
public class MoneyRecodeActivity extends BaseActivity implements GameMemberContract.View {
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private String emchatId;
    private String orderId;
    private String orderState;

    private RVAdapter<GameDetailResponse.UsersBean> mAdapter;
    private GameMemberPresenter mGameMemberPresenter;
    private GameDetailResponse response;

    public static void start(Activity activity, GameDetailResponse response) {
        Intent intent = new Intent(activity, MoneyRecodeActivity.class);
        intent.putExtra("response", response);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_money_recode;
    }

    @Override
    protected void initInject() {
        mGameMemberPresenter = new GameMemberPresenter();
        initPresenters(mGameMemberPresenter);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("收款记录");
        response = (GameDetailResponse) getIntent().getSerializableExtra("response");
        if (response == null)
            return;
        orderId = response.getId();
        emchatId = response.getEmchatId();
        orderState = response.getOrderState();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new RVAdapter<>(new MoneyRecodeItem(this));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new RVAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                if (Integer.parseInt(orderState) < 2) {
                    FlashLib.showToast("请先完成组队");
                } else if (response.getIsSetRest() != 1) {
                    FlashLib.showToast("请先设置尾款");
                } else {
                    FlashLib.showToast("已发送催款消息");
                    GameDetailResponse.UsersBean data = mAdapter.getData(position);
                    setMessage(data.getName(), data.getUid());
                }

            }
        });

        mGameMemberPresenter.getMembersList(orderId);
    }

    @OnClick(R.id.tv_top_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setMembersData(List<GameDetailResponse.UsersBean> membersData) {
        mAdapter.setDatas(membersData);
    }

    private void setMessage(String atUserName, String atUserId) {
        String content = "@" + atUserName + "  本次游戏即将开始，请及时支付尾款";
        EMMessage message = EMMessage.createTxtSendMessage(content, emchatId);
        message.setChatType(EMMessage.ChatType.GroupChat);
        List<String> atMembers = new ArrayList<>();
        atMembers.add(atUserId);
        JSONArray atJson = new JSONArray(atMembers);
        // 设置消息的扩展为@群成员类型
        message.setAttribute(EaseConstant.MESSAGE_ATTR_AT_MSG, atJson); // @6001,6002
        EMClient.getInstance().chatManager().sendMessage(message);
    }
}
